package o2o.service.company.postman;

import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.company.postman.PostCompanySalaryInfo;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Jahnke【1029777564@qq.com】
 * @ClassName PostManSalaryService
 * @Description: 配送员工资计算
 * @date 2019/8/22 17:14
 * @Version 1.0
 */
public class PostManSalaryService {

    private PostManSalaryService() {
    }

    enum Singleton {
        INSTANCE;
        private PostManSalaryService single;

        private Singleton() {
            single = new PostManSalaryService();
        }

        private PostManSalaryService getInstance() {
            return single;
        }
    }

    public static PostManSalaryService getInstance() {
        return Singleton.INSTANCE.single;
    }

    private static Map<String, PostCompanySalaryInfo> dataMap = new ConcurrentHashMap<>();

    // 配送员增加工资信息
    public void addSalary2User(String username, Long companyId, Double deliveryMoney, Double allowance, Connection conn) throws Exception {
        PostCompanySalaryInfo salaryInfo = this.getSalary(username, companyId);
        if (allowance != null && allowance > 0) {
            salaryInfo.setAllowance(salaryInfo.getAllowance() + allowance);
        }
        if (deliveryMoney == null || deliveryMoney < 0) {
            throw new Exception("请输入正确的配送金额");
        }
        salaryInfo.setMoney(salaryInfo.getMoney() + deliveryMoney);
        this.updateSalary(salaryInfo, conn);

        dataMap.remove(getKey(username, companyId));
    }

    // 获取用户在当前配送公司的账户信息
    public PostCompanySalaryInfo getSalary(String username, Long companyId) throws SQLException {
        String key = null;
        if (dataMap.containsKey(key = getKey(username, companyId))) {
            return dataMap.get(key);
        }
        String sql = "select * from " + PojoTables.POST_COMPANY_SALARY_INFO + " where username='" + username + "' and companyId=" + companyId;
        PostCompanySalaryInfo salaryInfo = (PostCompanySalaryInfo) UserDaoSql.getInstance().getObject(PostCompanySalaryInfo.class, sql);
        if (salaryInfo == null) {
            salaryInfo = new PostCompanySalaryInfo();
            salaryInfo.setAllowance(0D).setCompanyId(companyId).setUsername(username).setMoney(0D).setDelFlag(0).setCreateTime(System.currentTimeMillis());
            salaryInfo.setApplyMoney(0D).setPayedMoney(0D);
            long id = UserDaoSql.getInstance().saveObj(PojoTables.POST_COMPANY_SALARY_INFO, salaryInfo);
            salaryInfo.setId(id);
            dataMap.put(key, salaryInfo);
        }
        return salaryInfo;
    }

    // 判断是否可以提现
    public boolean canApplyMoney(String username, Long companyId, Double money) throws SQLException {
        PostCompanySalaryInfo info = this.getSalary(username, companyId);

        // 就算当前可以提现的金额
        double totalYuE = info.getAllowance() + info.getMoney() - (info.getApplyMoney() == null ? 0 : info.getApplyMoney());
        if (totalYuE >= money) {
            return true;
        }
        return false;
    }

    // 增加账户的待提现金额
    public void addApplyMoney(String username, Long companyId, Double money, Connection conn) throws Exception {
        PostCompanySalaryInfo info = this.getSalary(username, companyId);
        double totalYuE = info.getAllowance() + info.getMoney() - (info.getApplyMoney() == null ? 0 : info.getApplyMoney());
        if (totalYuE < money) {
            throw new Exception("账户余额不足，无法提现！");
        }
        info.setApplyMoney(info.getApplyMoney() + money);
        this.updateSalary(info, conn);
        dataMap.remove(getKey(username, companyId));
    }

    // 提现金额通过
    public synchronized void applyState(String username, Long companyId, Double money, boolean isPass, Connection conn) throws Exception {
        PostCompanySalaryInfo info = this.getSalary(username, companyId);
        if (isPass) {       // 通过
            double totalYuE = info.getAllowance() + info.getMoney();
            if (totalYuE < money) {
                throw new Exception("账户余额不足，无法通过！");
            }
            if (info.getApplyMoney() <= money) {
                info.setApplyMoney(0D);
            }
            if (info.getAllowance() > money) {
                info.setAllowance(info.getAllowance() - money);
            } else {
                double kc = money - info.getAllowance();
                info.setAllowance(0D);
                info.setMoney(info.getMoney() - kc);
            }
            info.setPayedMoney(info.getPayedMoney() + money);
        } else {
            // 被拒绝  减去正在申请提现的金额即可
            info.setApplyMoney(info.getApplyMoney() <= money ? 0 : info.getApplyMoney() - money);
        }
        this.updateSalary(info, conn);
        dataMap.remove(getKey(username, companyId));
    }

    // 更新数据
    private void updateSalary(PostCompanySalaryInfo salaryInfo, Connection conn) throws SQLException {
        Map<String, Object> querys = new HashMap<>();
        querys.put("companyId", salaryInfo.getCompanyId());
        querys.put("username", salaryInfo.getUsername());

        if (null == conn) {
            UserDaoSql.getInstance().updateObj(PojoTables.POST_COMPANY_SALARY_INFO, salaryInfo, querys);
        } else {
            UserDaoSql.getInstance().updateObj(PojoTables.POST_COMPANY_SALARY_INFO, salaryInfo, querys, conn);
        }

    }

    private static String getKey(String username, Long companyId) {
        return "user_salary_" + username + "_" + companyId;
    }
}
